/**
    Copyright (c) 2009 yakiimo02
    Distributed under the New BSD License.
    See included license.txt or http://www.yakiimo3d.com/NewBSDLicense.txt
**/

#include "DXUT.h"
#include "BitonicSortLoopCPU.h"

/**
    Sort m_data in increasing order.
*/
void BitonicSortLoopCPU::SortUp()
{
    SortUp( m_data.size() );
    CheckSortUp( m_data );
}

/**
    Sort m_data in decreasing order.
*/
void BitonicSortLoopCPU::SortDown()
{
    SortDown( m_data.size() );
    CheckSortDown( m_data );
}

/**
    @param nCount    [in]        The number of elements to sort.
*/
void BitonicSortLoopCPU::SortUp( int nCount )
{
    // loop from Merge( 2 ) to Merge( nCount )
    for( int nMergeSize=2; nMergeSize<=nCount; nMergeSize=nMergeSize*2 ) 
    {
        // Merge( nCount ) requires log2( nCount ) merges. Merge( nCount/2 ) -> Merge( 2 )
        for( int nMergeSubSize=nMergeSize>>1; nMergeSubSize>0; nMergeSubSize=nMergeSubSize>>1 ) 
        {
            // compare and swap elements
            for( int nElem=0; nElem<nCount; ++nElem ) 
            {
                int nSwapElem = nElem^nMergeSubSize;
                // check to make sure to only swap once
                if( nSwapElem > nElem ) 
                {
                    // sort in increasing order
                    if ( ( nElem & nMergeSize ) == 0 && m_data[ nElem ]>m_data[ nSwapElem ] ) { 
                        Swap( nElem, nSwapElem ); 
                    }
                    // sort in descending order
                    if ( ( nElem & nMergeSize ) != 0 && m_data[ nElem ]<m_data[ nSwapElem ] ) { 
                        Swap( nElem, nSwapElem ); 
                    }
                }
            }
        }
    }
}

/**
    @param nCount    [in]        The number of elements to sort.
*/
void BitonicSortLoopCPU::SortDown( int nCount )
{
    // loop from Merge( 2 ) to Merge( nCount )
    for( int nMergeSize=2; nMergeSize<=nCount; nMergeSize=nMergeSize*2 ) 
    {
        // Merge( nCount ) requires log2( nCount ) merges. Merge( nCount/2 ) -> Merge( 2 )
        for( int nMergeSubSize=nMergeSize>>1; nMergeSubSize>0; nMergeSubSize=nMergeSubSize>>1 ) 
        {
            // compare and swap elements
            for( int nElem=0; nElem<nCount; ++nElem ) 
            {
                int nSwapElem = nElem^nMergeSubSize;
                // check to make sure to only swap once
                if( nSwapElem > nElem ) 
                {
                    // sort in descending order
                    if ( ( nElem & nMergeSize ) == 0 && m_data[ nElem ]<m_data[ nSwapElem ] ) { 
                        Swap( nElem, nSwapElem ); 
                    }
                    // sort in increasing order
                    if ( ( nElem & nMergeSize ) != 0 && m_data[ nElem ]>m_data[ nSwapElem ] ) { 
                        Swap( nElem, nSwapElem ); 
                    }
                }
            }
        }
    }
}
